//
//  AddCarDetViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/10/13.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "AddCarDetViewController.h"

#import "CarDetCell.h"
#import "AddCarManaView.h"
@interface AddCarDetViewController ()<UITableViewDelegate,UITableViewDataSource>{
    NSMutableArray *array;
    NSDictionary *dic;
}

@property (nonatomic,strong)UITableView *tableView;

@end

@implementation AddCarDetViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setBGColor];
    [self showNav:@"商品详情"];
    array = [NSMutableArray array];
    
    [self loadData];
    
}

- (void)loadUI{
    AddCarManaView *views = [[[NSBundle mainBundle] loadNibNamed:@"AddCarManaView" owner:nil options:nil] lastObject];
    views.frame = CGRectMake(0, 0, ScreenWidth, 830);
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    _tableView.tableHeaderView = views;
    
    NSString *carthemephoto = [dic objectForKey:@"carthemephoto"];
    NSArray *arr = [carthemephoto componentsSeparatedByString:@"|"];
    NSMutableArray *images = [NSMutableArray array];
    for (NSString *aaa in arr) {
        NSString *imgUrl = [NSString stringWithFormat:@"%@%@",[user objectForKey:@"ImageUrl"],aaa];
        [images addObject:imgUrl];
    }
    
    views.image1.hidden = YES;
    views.labZP.hidden = YES;
    views.labZY.hidden = YES;
    UIView *v = [WTImageScroll ShowNetWorkImageScrollWithFream:CGRectMake(0, 0, ScreenWidth, 200) andImageArray:images andBtnClick:^(NSInteger tagValue) {
        
    }];
    [views.views addSubview:v];
    
    views.text1.enabled = NO;
    views.text2.enabled = NO;
    views.text3.enabled = NO;
    views.text4.enabled = NO;
    views.text5.enabled = NO;
    views.text6.enabled = NO;
    views.text7.enabled = NO;
    
    views.text1.text = [dic objectForKey:@"brand"];
    NSString *type = [NSString stringWithFormat:@"%li",[[dic objectForKey:@"type"] integerValue]];
    if ([type integerValue] == 1) {
        views.lab1.text = @"准新车";
    }else{
        views.lab1.text = @"新车";
    }
    
    views.text2.text = [dic objectForKey:@"sellingprice"];
    views.text3.text = [dic objectForKey:@"guideprice"];
    views.text4.text = [dic objectForKey:@"carcolor"];
    views.text5.text = [dic objectForKey:@"customerservice"];
    views.lab2.text = views.lab1.text;
    views.text6.text = [dic objectForKey:@"travel"];
    
    views.lab3.text = [dic objectForKey:@"thefactorytime"];
    views.lab4.text = [dic objectForKey:@"address"];
    views.text7.text = [dic objectForKey:@"detailedaddress"];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"Cell";
    CarDetCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"CarDetCell" owner:nil options:nil] lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *op = array[indexPath.row];
    NSString *imgUrl = [NSString stringWithFormat:@"%@%@",[user objectForKey:@"ImageUrl"],[op objectForKey:@"img"]];
    [cell.images sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:[UIImage imageNamed:@""]];
    cell.text.text = [op objectForKey:@"details"];
    [cell.text setEditable:NO];
    cell.text.backgroundColor = [UIColor grayColor];
    
    cell.imgPC.hidden = YES;
    cell.labSC.hidden = YES;
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 350.0;
}
//
- (void)loadData{
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:_productId forKey:@"id"];
    [parmas setObject:@"0" forKey:@"longitude"];
    [parmas setObject:@"0" forKey:@"latitude"];
    
    NSString *url = [NSString stringWithFormat:@"%@/App/CarBuying/details",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"res:%@",responseObject);
        NSString *error = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"error"] integerValue]];
        if ([error integerValue] == 200) {
            dic = [responseObject objectForKey:@"result"];
            NSArray *arr = [dic objectForKey:@"detailsa"];
            if ([arr isKindOfClass:[NSArray class]]) {
                array = [arr mutableCopy];
            }
            [_tableView reloadData];
            [self loadUI];
        }
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
